//
//  PYWaterFallFlowLayout.m
//  WaterFallStream
//
//  Created by zpy on 15/12/5.
//  Copyright © 2015年 zpy. All rights reserved.
//

#import "PYWaterFallFlowLayout.h"

#define PYCollectionViewWidth self.collectionView.frame.size.width
// 默认行间距
static const CGFloat PYDefaultRowSpacing = 10;
//
static const CGFloat PYDefaultColumnSpacing = 10;
//
static const UIEdgeInsets PYDefaultEdgeInsets = {10,10,10,10};
//
static const NSUInteger PYDefaultColumnCount = 3;

@interface PYWaterFallFlowLayout()
// 每一列的最大Y坐标
@property(nonatomic,strong)NSMutableArray *columnMaxYArray;
@property(nonatomic,strong)NSMutableArray *attrsArray;
// 声明get方法
-(CGFloat)rowSpacing;
-(CGFloat)columnSpacing;
-(NSUInteger)columnCount;
-(UIEdgeInsets)edgeInsets;

@end
@implementation PYWaterFallFlowLayout
// 懒加载
-(NSMutableArray *)columnMaxYArray{

    if (_columnMaxYArray == nil) {
        _columnMaxYArray = [[NSMutableArray alloc] init];
    }
    return _columnMaxYArray;
}
-(NSMutableArray *)attrsArray{

    if (_attrsArray == nil) {
        _attrsArray = [[NSMutableArray alloc] init];
    }
    return _attrsArray;
}
/**
 *  决定collectionView的contentsize
 */
// 找出最长那一列的最大的Y值
-(CGSize)collectionViewContentSize{

    CGFloat destColumnMaxY = [self.columnMaxYArray[0] doubleValue];
    for (int i = 1; i < self.columnMaxYArray.count; i ++) {
        CGFloat columnMaxY = [self.columnMaxYArray[i] doubleValue];
        if (columnMaxY > destColumnMaxY) {
            destColumnMaxY = columnMaxY;
        }
    }
    return CGSizeMake(PYCollectionViewWidth, destColumnMaxY + self.edgeInsets.bottom);
}
-(void)prepareLayout{
    [super prepareLayout];
// 重置每一列的最大Y坐标
    [self.columnMaxYArray removeAllObjects];
    for (int i = 0; i < self.columnCount; i ++) {
        [self.columnMaxYArray addObject:@(self.edgeInsets.top)];
    }
    // 计算所有cell的布局属性
    [self.attrsArray removeAllObjects];
    NSUInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < count; i ++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }
}
/**
 *  所有元素最终显示出来的布局属性
 */
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.attrsArray.count; i ++) {
        UICollectionViewLayoutAttributes *attrs = self.attrsArray[i];
        
        if (CGRectIntersectsRect(rect, attrs.frame)) {
            [array addObject:attrs];
        }
    }
    return array;
}
/**
 *  说明indexpath 位置cell 的布局属性
 */
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    // 计算indexpath位置对应的cell
    // 找出  最短那一行对应的 最大的Y值
    CGFloat destColumnMaxY = [self.columnMaxYArray[0] doubleValue];
    NSUInteger destColumnIndex = 0;
    for (int i = 0; i < self.columnMaxYArray.count; i ++ ) {
        CGFloat columnMaxY = [self.columnMaxYArray[i] doubleValue];
        if (columnMaxY < destColumnMaxY) {
            destColumnMaxY = columnMaxY;
            destColumnIndex = i;
        }
    }
        CGFloat totalColumnSpacing = (self.columnCount - 1) * PYDefaultColumnSpacing;
        CGFloat width = (PYCollectionViewWidth - self.edgeInsets.left - self.edgeInsets.right - totalColumnSpacing) / self.columnCount;
        
        CGFloat height = [self.delegate waterFlowLayout:self heightForItemAtIndexpath:indexPath withItemWidth:width];
        CGFloat x = self.edgeInsets.left + destColumnIndex * (width + self.columnSpacing);
        CGFloat y = destColumnMaxY;
        if (destColumnMaxY != self.edgeInsets.top) {
            y += self.rowSpacing;
        }
        attrs.frame = CGRectMake(x, y, width, height);
        // 更新最大y坐标
        self.columnMaxYArray[destColumnIndex] = @(CGRectGetMaxY(attrs.frame));
        
        return attrs;
    }
#pragma mark 处理 代理数据
-(CGFloat)rowSpacing{

    if ([self.delegate respondsToSelector:@selector(rowSpacingInWaterFallFlowLayout:)]) {
        return [self.delegate rowSpacingInWaterFallFlowLayout:self];
    }
    return PYDefaultRowSpacing;
}
-(CGFloat)columnSpacing{

    if ([self.delegate respondsToSelector:@selector(columnSpacingInWaterFallFlowLayout:)]) {
        return [self.delegate columnSpacingInWaterFallFlowLayout:self];
    }
    return PYDefaultColumnSpacing;
}
/// Returning 'double _Nullable' from a function with incompatible result type 'UIEdgeInsets' (aka 'struct UIEdgeInsets')
-(UIEdgeInsets)edgeInsets{

    if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterFallFlowLayout:)]) {
        return [self.delegate edgeInsetsInWaterFallFlowLayout:self];
    }
    return PYDefaultEdgeInsets;
}
-(NSUInteger)columnCount{

    if ([self.delegate respondsToSelector:@selector(columnCountInWaterFallFlowLayout:)]) {
        return [self.delegate columnCountInWaterFallFlowLayout:self];
    }
    return PYDefaultColumnCount;
}
@end
